package com.disk.utils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.disk.entity.constants.Constants;
import com.disk.entity.dto.SysSettingDto;
import com.disk.entity.dto.UserSpaceDto;
import com.disk.entity.po.FileInfo;
import com.disk.mapper.FileInfoMapper;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class RedisComponent {

    @Resource
    private RedisOperationUtils redisUtils;

    @Resource
    private FileInfoMapper fileInfoMapper;

    // 获取系统设置
    public SysSettingDto getSysSettingDto() {
        SysSettingDto sysSettingDto = (SysSettingDto) redisUtils.get(Constants.REDIS_KEY_SYS_SETTING);
        if (sysSettingDto == null) {
            sysSettingDto = new SysSettingDto();
            redisUtils.set(Constants.REDIS_KEY_SYS_SETTING, sysSettingDto);
        }
        return sysSettingDto;
    }

    public void saveSysSettingsDto(SysSettingDto sysSettingDto) {
        redisUtils.set(Constants.REDIS_KEY_SYS_SETTING, sysSettingDto);
    }

    public void saveUserSpaceUse(String userId, UserSpaceDto userSpaceDto) {
        redisUtils.setExpire(Constants.REDIS_KEY_USER_SPACE_USE + userId, userSpaceDto, Constants.REDIS_KEY_EXPIRES_DAY);
    }

    public UserSpaceDto getUserSpaceUse(String userId) {
        UserSpaceDto spaceDto = (UserSpaceDto) redisUtils.get(Constants.REDIS_KEY_USER_SPACE_USE + userId);
        if (null == spaceDto) {
            spaceDto = new UserSpaceDto();
            // 获取用户已上传文件的占用空间
            List<Long> fileSizes = fileInfoMapper.selectList(new LambdaQueryWrapper<FileInfo>()
                            .eq(FileInfo::getUserId, userId)
                            .select(FileInfo::getFileSize))
                    .stream()
                    .map(FileInfo::getFileSize)
                    .collect(Collectors.toList());
            Long useSpace = fileSizes.stream().mapToLong(Long::longValue).sum();

            spaceDto.setUseSpace(useSpace);
            spaceDto.setTotalSpace(getSysSettingDto().getUserInitSpace() * Constants.MB);
            saveUserSpaceUse(userId, spaceDto);
        }
        return spaceDto;
    }


    public void saveFileTempSize(String userId, String fileId, Long fileSize) {
        Long currentSize = getFileTempSize(userId, fileId);
        redisUtils.setExpire(Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + "_" + fileId, currentSize + fileSize, Constants.REDIS_KEY_EXPIRES_ONE_HOUR);
    }


    //获取临时文件大小
    public Long getFileTempSize(String userId, String fileId) {
        Long currentSize = getFileSizeFromRedis(Constants.REDIS_KEY_USER_FILE_TEMP_SIZE + userId + "_" + fileId);
        return currentSize;
    }

    private Long getFileSizeFromRedis(String key) {
        Object size = redisUtils.get(key);
        if (null == size)
            return 0L;
        if (size instanceof Long)
            return (Long) size;
        if (size instanceof Integer)
            return ((Integer) size).longValue();
        return 0L;
    }
}
